#include "borrinformation.h"
#include "ui_borrinformation.h"
#include "QMessageBox"
borrInformation::borrInformation(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::borrInformation),
    add_dialog(nullptr),
    mod_dialog(nullptr)

{
    ui->setupUi(this);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    QVector<QStringList> stringlist = fileWriteAndRead().readAndParseFile("student.txt");
//    qDebug()<<stringlist;
    for(int i = 0; i < stringlist.size();i++)
    {
        Student stu(stringlist[i]);
        this->student.insert(stu.stu_number,stu);
    }
    stringlist.clear();
    stringlist = fileWriteAndRead().readAndParseFile("book.txt");
//    qDebug()<<stringlist;
    for(int i = 0; i < stringlist.size();i++)
    {
        Book bok(stringlist[i]);
        this->book.insert(bok.book_number,bok);
    }
//    /*qDebug*/()<<__LINE__<<book.begin()->book_array.size();
//    qDebug()<<stringlist;
    stringlist.clear();
    stringlist = fileWriteAndRead().readAndParseFile("borrowing.txt");
//    qDebug()<<stringlist;
    for(int i = 0; i < stringlist.size();i++)
    {
        Borrowing bor(stringlist[i]);
//        qDebug()<<bor.bor_number<<" "<<bor.stu_number<<" "<<bor.book_number<<" "<<bor.bor_BorrowingTime.toString("yyyy年MM月dd日")<<" " <<bor.bor_Return.toString("yyyy年MM月dd日");
        this->borrowing.insert(bor.bor_number,bor);

    }
//    qDebug()<<__LINE__<<endl;
//    qDebug()<<borrowing.size();
//    qDebug()<<student.size();
    for(int i = 0; i < BORROWING;i++)
    {
        ui->tableWidget->insertColumn(i);
    }
//     qDebug()<<__LINE__<<endl;
    printfTable();
//    Borrowing bor("2031053231203105323100001","2031053231","203105323100001",QDate::currentDate(),QDate::currentDate());
//    qDebug()<<bor.getBorrowing();

//    borrowing.insert(bor.bor_number,bor);
}

borrInformation::~borrInformation()
{
//    QVector<QStringList> cnt;
//    for(Student& stu:this->student)
//    {
//        cnt.push_back(stu.getStudent());
//    }
//    fileWriteAndRead().writeDataToFile("student.txt",cnt);
//    cnt.clear();

//    for(Book& bok:this->book)
//    {
//        cnt.push_back(bok.getBook());
//    }
//    fileWriteAndRead().writeDataToFile("book.txt",cnt);
//    cnt.clear();

//    for(Borrowing& bor:this->borrowing)
//    {
//        cnt.push_back(bor.getBorrowing());
//    }
//    fileWriteAndRead().writeDataToFile("borrowing.txt",cnt);
//    cnt.clear();

    delete ui;
}

void borrInformation::printfTable()
{
//     qDebug()<<__LINE__<<endl;
    while(ui->tableWidget->rowCount()>0)
    {
        ui->tableWidget->removeRow(ui->tableWidget->rowCount()-1);
    }
// qDebug()<<__LINE__<<endl;
    for(int i = 0; i < borrowing.size();i++)
    {
        ui->tableWidget->insertRow(i);
    }

    ui->tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("借阅号"));
    ui->tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("学号"));
    ui->tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("书号"));
    ui->tableWidget->setHorizontalHeaderItem(3, new QTableWidgetItem("借阅时间"));
    ui->tableWidget->setHorizontalHeaderItem(4, new QTableWidgetItem("归还时间"));
    ui->tableWidget->setHorizontalHeaderItem(5, new QTableWidgetItem("备注"));

    int i = 0;
    for(auto j = borrowing.begin(); j!=borrowing.end();j++,i++)
    {
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(j.value().bor_number));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(j.value().stu_number));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(j.value().book_number));
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(j.value().bor_BorrowingTime.toString("yyyy年MM月dd日")));
        ui->tableWidget->setItem(i, 4, new QTableWidgetItem(j.value().bor_Return == QDate(1000,1,1) ? QString("未归还") : j.value().bor_Return.toString("yyyy年MM月dd日")));


        auto item = book.find(j.value().book_number);
        if(item !=book.end())
        {
            auto& arr = item->book_array;
//            qDebug()<<__LINE__<<arr.size();
            for(int z = 0; z < arr.size();z++)
            {
//                qDebug()<<__LINE__<<arr[z].book_number<<arr[z].book_Remarks<<endl;
                if(arr[z].book_number == j.value().bor_number.right(j.value().bor_number.size()-j.value().stu_number.size()))
                {
//                    qDebug()<<__LINE__<<__LINE__<<arr[z].book_number<<arr[z].book_Remarks<<endl;
                    QString cnt = arr[z].book_Remarks == "" ? QString("无"): arr[z].book_Remarks;
//                    qDebug()<<cnt<<endl;
                    ui->tableWidget->setItem(i, 5, new QTableWidgetItem(cnt));
                }
            }

        }

//        ui->tableWidget->setItem(i, 5, new QTableWidgetItem()))
    }
}

void borrInformation::closeEvent(QCloseEvent *event)
{
    this->close();
    emit is_close();
}

void borrInformation::on_pushButton_add_clicked()
{
    if(add_dialog==nullptr)
    {
        add_dialog = new bor_add_Dialog(this);
        connect(add_dialog,&bor_add_Dialog::stu_number,this,&borrInformation::send_stu_number);
        connect(add_dialog,&bor_add_Dialog::book_number,this,&borrInformation::send_book_number);
    }

    add_dialog->exec();

    if(add_dialog->flag == true)
    {
        QMessageBox* message = new QMessageBox(this);
        QStringList cnt = add_dialog->borr;
        borrowing.insert(cnt[0],Borrowing(cnt));
        message->setText("借阅成功");
        this->student[add_dialog->student[0]].stu_bookNum = add_dialog->student[4].toUInt();

        auto& arr = this->book[add_dialog->book[0]].book_array;
        for(int i = 0; i < arr.size();i++)
        {
            int n = add_dialog->student[0].size();
            if(arr[i].book_number==cnt[0].right(cnt[0].size()-n))
            {
                arr[i].book_state = Book_state::Lending;
                arr[i].book_Remarks="";
            }
        }
        message->exec();
    }

    printfTable();
    add_dialog->disconnect();
    delete add_dialog;
    add_dialog=nullptr;
}

void borrInformation::send_stu_number(const QString& stu)
{
    add_dialog->student=student[stu].getStudent();
    //    qDebug()<<add_dialog->student<<"105";
}

void borrInformation::send_book_number(const QString &bok)
{
    if(book.find(bok) != book.end())
    {
        add_dialog->book=book[bok].getBook();
    }

//    qDebug()<<add_dialog->book;
}

void borrInformation::on_pushButton_mod_clicked()
{
    if(mod_dialog==nullptr)
    {
        mod_dialog = new bor_mod_dialog(this);
//        connect(add_dialog,&bor_add_Dialog::stu_number,this,&borrInformation::send_stu_number);
//        connect(add_dialog,&bor_add_Dialog::book_number,this,&borrInformation::send_book_number);
    }

//    mod_dialog->exec();
    int row = ui->tableWidget->currentRow();
//    qDebug()<<row;
    if(row != -1)
    {
//        qDebug()<<ui->tableWidget->item(row,0)->text();
        auto& value = borrowing[ui->tableWidget->item(row,0)->text()];
        mod_dialog->borrwing = value.getBorrowing();
        mod_dialog->book=book[value.book_number].getBook();
//        qDebug()<<__LINE__<<"   "<<mod_dialog->book;
        mod_dialog->student = student[value.stu_number].getStudent();
//        qDebug()<< mod_dialog->student<<endl;
        mod_dialog->printfData();
//        mod_dialog->book.clear();
        mod_dialog->exec();

        if(mod_dialog->flag == true)
        {
            QMessageBox* message = new QMessageBox(this);
            QStringList stu = mod_dialog->student;
//            qDebug()<<stu<<endl;
            QStringList bok = mod_dialog->book;
//            qDebug()<<__LINE__<<"  "<<bok;
            QStringList borr = mod_dialog->borrwing;
            student.insert(stu[0], Student(stu));
//            qDebug()<<__LINE__<<"  "<<book.begin().value().book_array.size();
            book[bok[0]] = Book(bok);
//            qDebug()<<__LINE__<<"  "<<book.begin().value().book_array.size();
            borrowing.insert(borr[0],Borrowing(borr));
            message->setText("归还成功");
            message->exec();
        }
    }



    printfTable();
//    add_dialog->disconnect();
    delete mod_dialog;
    mod_dialog=nullptr;
}

void borrInformation::on_pushButton_close_clicked()
{
    this->close();
}

void borrInformation::on_pushButton_save_clicked()
{
    QVector<QStringList> cnt;
    for(Student& stu:this->student)
    {
        cnt.push_back(stu.getStudent());
    }
    fileWriteAndRead().writeDataToFile("student.txt",cnt);
    cnt.clear();

    for(Book& bok:this->book)
    {
        cnt.push_back(bok.getBook());
    }
//    qDebug()<<__LINE__<< "   " << cnt<<endl;
    fileWriteAndRead().writeDataToFile("book.txt",cnt);
    cnt.clear();

    for(Borrowing& bor:this->borrowing)
    {
        cnt.push_back(bor.getBorrowing());
    }
    fileWriteAndRead().writeDataToFile("borrowing.txt",cnt);
    cnt.clear();
}

void borrInformation::on_pushButton_delet_clicked()
{
    int row = ui->tableWidget->currentRow();
//    qDebug()<<row;
    if(row != -1)
    {
//        qDebug()<<ui->tableWidget->item(row,0)->text();
        borrowing.remove(ui->tableWidget->item(row,0)->text());
//        book.remove(ui->tableWidget->item(row,0)->text());

        ui->tableWidget->removeRow(row);
    }

    printfTable();
}
